//
//  collectTableViewController.m
//  A1.0
//
//  Created by lanou3g on 15-4-2.
//  Copyright (c) 2015年 ccandxx. All rights reserved.
//

#import "collectTableViewController.h"
#import "MyInterface.h"

#import "jokeModel.h"
#import "picModel.h"
#import "womenModel.h"
#import "videoModel.h"
#import "petModel.h"

#import "ImgViewController.h"
#import "PicDetailModel.h"
#import <UIKit/UIKit.h>
@interface collectTableViewController ()


@property(nonatomic,strong)NSMutableArray *allArray;
@property(nonatomic,strong)NSMutableArray *collectJoke;
@property(nonatomic,strong)NSMutableArray *collectFunny;
@property(nonatomic,strong)NSMutableArray *collectWomen;
@property(nonatomic,strong)NSMutableArray *collectVideo;
@property(nonatomic,strong)NSMutableArray *collectPet;


//记录位置
@property (nonatomic,assign) NSInteger jokeIndex;
@property (nonatomic,assign) NSInteger picIndex;
@property (nonatomic,assign) NSInteger womenIndex;
@property (nonatomic,assign) NSInteger videoIndex;
@property (nonatomic,assign) NSInteger petIndex;

@end

@implementation collectTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
        //添加segment
    NSArray *itemArray = @[@"趣闻",@"美妞",@"搞图",@"萌宠"];
    self.seg = [[UISegmentedControl alloc]initWithItems:itemArray];
    self.seg.frame = CGRectMake(70, 8, [UIScreen mainScreen].bounds.size.width - 120, 30);
    self.seg.selectedSegmentIndex = 0;
    
   
    
    [self.navigationController.navigationBar addSubview:self.seg];
    
    
    
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"collectCell"];
    
    //初始化
    self.allArray = [NSMutableArray array];
    
    //数组分别对应赋值
    self.collectJoke = [MyInterface selectWithAllJoke];
    self.collectFunny = [MyInterface selectWithAllFunny];
    self.collectWomen = [MyInterface selectWithAllWomen];
    self.collectVideo = [MyInterface selectWithAllVideo];
    self.collectPet = [MyInterface selectWithAllPet];
    
    //记录位置
    self.jokeIndex = self.collectJoke.count;
    self.picIndex = self.collectFunny.count;
    self.womenIndex = self.collectWomen.count;
    self.videoIndex = self.collectVideo.count;
    self.petIndex = self.collectPet.count;
    
    //添加到总数组
    [self.allArray addObjectsFromArray:_collectJoke];
    [self.allArray addObjectsFromArray:_collectFunny];
    [self.allArray addObjectsFromArray:_collectWomen];
    [self.allArray addObjectsFromArray:_collectVideo];
    [self.allArray addObjectsFromArray:_collectPet];
    
    
    //segment
    [self.seg addTarget:self action:@selector(segmentAction:) forControlEvents:(UIControlEventValueChanged)];
    
    //添加右键删除
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemTrash) target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
    
}


//右键
- (void)rightBarButtonAction:(UIBarButtonItem *)sender
{
    
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    
}

//segment
-(void)segmentAction:(UISegmentedControl *)sender
{
    if (self.seg.selectedSegmentIndex==9) {
        
        
        self.collectJoke = [MyInterface selectWithAllJoke];
        self.collectFunny = [MyInterface selectWithAllFunny];
        self.collectWomen = [MyInterface selectWithAllWomen];
        self.collectPet = [MyInterface selectWithAllPet];
        
        self.jokeIndex = self.collectJoke.count;
        self.picIndex = self.collectFunny.count;
        self.womenIndex = self.collectWomen.count;
        self.petIndex = self.collectPet.count;
        
        [self.allArray removeAllObjects];
        
        [self.allArray addObjectsFromArray:_collectJoke];
        [self.allArray addObjectsFromArray:_collectFunny];
        [self.allArray addObjectsFromArray:_collectWomen];
        [self.allArray addObjectsFromArray:_collectPet];
    }
    
    
    if (sender.selectedSegmentIndex == 0){
        self.collectJoke = [MyInterface selectWithAllJoke];
        
        self.jokeIndex = self.collectJoke.count;
    }
    if (sender.selectedSegmentIndex == 1){
        self.collectWomen = [MyInterface selectWithAllWomen];
        self.womenIndex = self.collectWomen.count;
    }
    
    if (sender.selectedSegmentIndex == 2){
        self.collectFunny = [MyInterface selectWithAllFunny];
        self.picIndex = self.collectFunny.count;
    }
    
    if (sender.selectedSegmentIndex == 3){
        self.collectPet = [MyInterface selectWithAllPet];
        self.petIndex = self.collectPet.count;
        
    }
    
    [self.tableView reloadData];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - 右键删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.seg.selectedSegmentIndex==9) {
        if (indexPath.row < self.jokeIndex) {
            
            jokeModel *j = _allArray[indexPath.row];
            
            [MyInterface deleteWithJokeID:j.jid];
            
            self.jokeIndex--;
            
            [self.allArray removeObjectAtIndex:indexPath.row];
        }else if ((indexPath.row > self.jokeIndex) && (indexPath.row < (self.jokeIndex + self.womenIndex))){
            
            womenModel *w = self.allArray[indexPath.row];
            
            [MyInterface deleteWithWomenID:w.wid];
            
            self.womenIndex--;
            
            [self.allArray removeObjectAtIndex:indexPath.row];
            
        }else if ((indexPath.row > (self.jokeIndex + self.womenIndex)) && (indexPath.row < (self.jokeIndex + self.womenIndex + self.picIndex))){
            
            picModel *pic = self.allArray[indexPath.row];
            
            [MyInterface deleteWithFunnyID:pic.fid];
            
            self.picIndex--;
            
            [self.allArray removeObjectAtIndex:indexPath.row];
            
        }else {
            petModel *pet = self.allArray[indexPath.row];
            
            [MyInterface deleteWithPetID:pet.pid];
            
            
            [self.allArray removeObjectAtIndex:indexPath.row];
            
        }
        
    }
    
    
    if (self.seg.selectedSegmentIndex==0) {
        
        jokeModel *joke = self.collectJoke[indexPath.row];
        [MyInterface deleteWithJokeID:joke.jid];
        [self.collectJoke removeObjectAtIndex:indexPath.row];
        
        
    }
    
    if (self.seg.selectedSegmentIndex == 1) {
        womenModel *wo = self.collectWomen[indexPath.row];
        [MyInterface deleteWithWomenID:wo.wid];
        [self.collectWomen removeObjectAtIndex:indexPath.row];
    }
    
    if (self.seg.selectedSegmentIndex == 2) {
        picModel *p = self.collectFunny[indexPath.row];
        [MyInterface deleteWithFunnyID:p.fid];
        [self.collectFunny removeObjectAtIndex:indexPath.row];
    }
    
    if (self.seg.selectedSegmentIndex == 3) {
        petModel *pe = self.collectPet[indexPath.row];
        [MyInterface deleteWithPetID:pe.pid];
        [self.collectPet removeObjectAtIndex:indexPath.row];
    }
    
    
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
}





#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.seg.selectedSegmentIndex==9) {
        return self.allArray.count;
    }else if (self.seg.selectedSegmentIndex==0) {
        return self.collectJoke.count;
    }else if (self.seg.selectedSegmentIndex == 1){
        return self.collectWomen.count;
    }else if (self.seg.selectedSegmentIndex == 2){
        return self.collectFunny.count;
    }else if (self.seg.selectedSegmentIndex == 3)
    {
        return self.collectPet.count;
    }else
    {
        return 0;
    }
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectCell" forIndexPath:indexPath];
    
    cell.textLabel.numberOfLines = 0;
    
    
    if (self.seg.selectedSegmentIndex == 9) {
        if (indexPath.row < self.jokeIndex) {
            cell.textLabel.text = ((jokeModel*)self.allArray[indexPath.row]).title;
        }else if ((indexPath.row > self.jokeIndex) && (indexPath.row < (self.jokeIndex + self.womenIndex))){
            
            cell.textLabel.text = ((womenModel*)self.allArray[indexPath.row]).title;
            
        }else if ((indexPath.row > (self.jokeIndex + self.womenIndex)) && (indexPath.row < (self.jokeIndex + self.womenIndex + self.picIndex))){
            
            cell.textLabel.text = ((picModel*)self.allArray[indexPath.row]).title;
            
        }else if ( indexPath.row > (self.jokeIndex + self.womenIndex + self.picIndex)){
            cell.textLabel.text = ((petModel*)self.allArray[indexPath.row]).text;
            
        }else
        {
            
        }
        
    }
    
    if (self.seg.selectedSegmentIndex == 0) {
        
        cell.textLabel.text = ((jokeModel*)self.collectJoke[indexPath.row]).title;
    }
    
    if (self.seg.selectedSegmentIndex==1) {
        cell.textLabel.text = ((womenModel*)self.collectWomen[indexPath.row]).title;
    }
    
    if (self.seg.selectedSegmentIndex==2) {
        cell.textLabel.text = ((picModel*)self.collectFunny[indexPath.row]).title;
    }
    
    if (self.seg.selectedSegmentIndex==3) {
        cell.textLabel.text = ((petModel*)self.collectPet[indexPath.row]).text;
    }
    
    return cell;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.seg removeFromSuperview];
    [self.navigationController popToRootViewControllerAnimated:NO];
}

//返回cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.seg.selectedSegmentIndex == 0) {
        
        jokeModel *m = [[jokeModel alloc]init];
        m = self.collectJoke[indexPath.row];
        
        //label自适应
        CGRect rect = [m.title boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 2000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil];
        
        CGFloat h =  rect.size.height;
        
        return h +30;

        
    }
    return 44;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.seg.selectedSegmentIndex == 0) {
        
        
    }
    
    if (self.seg.selectedSegmentIndex==1) {

        womenModel *m = [[womenModel alloc]init];
        m = self.collectWomen[indexPath.row];
        PicDetailModel *pm = [[PicDetailModel alloc]init];
        
        pm.pic_h = m.pic_h;
        pm.pic_w = m.pic_w;
        pm.pic = m.pic;
        
        ImgViewController *dvc = [[ImgViewController alloc]init];
        dvc.model = pm;
        
        [self presentViewController:dvc animated:YES completion:nil];
    
            }
    
    if (self.seg.selectedSegmentIndex==2) {
        picModel *m = [[picModel alloc]init];
        m = self.collectFunny[indexPath.row];
        
        PicDetailModel *pm = [[PicDetailModel alloc]init];
        
        pm.pic_h = m.pic_h;
        pm.pic_w = m.pic_w;
        pm.pic = m.pic;
        
        ImgViewController *dvc = [[ImgViewController alloc]init];
        dvc.model = pm;
        
        [self presentViewController:dvc animated:YES completion:nil];

    }
    
    if (self.seg.selectedSegmentIndex==3) {
        petModel *m = [[petModel alloc]init];
        m = self.collectPet[indexPath.row];
        PicDetailModel *pm = [[PicDetailModel alloc]init];
        
        pm.pic_h = m.height;
        pm.pic_w = m.width;
        pm.pic = m.original;
        
        ImgViewController *dvc = [[ImgViewController alloc]init];
        dvc.model = pm;
        
        [self presentViewController:dvc animated:YES completion:nil];

    }

}

@end
